library(tidyr)
library(tidyverse)
library(ggplot2)
library(readstata13)
library(patchwork)

##2018 CCES Dataset#### 

#Import Data 
data1<-read.dta13("2018CCES.dta")

#Subset to what is needed 
data1 <- data1 %>% 
  select(rep_approve, dem_approve, pid3_lean, treat_democrats, treat_republicans, profile_order)
summary(data1)

#Code treatment for Republican experiment 
data1$treat_republicans<-factor(data1$treat_republicans,
                                levels=c(1,2,3,4),
                                labels=c("Conservative;\n Supports Trump",
                                         "Conservative;\n Critiques Trump",
                                         "Moderate;\n Supports Trump",
                                         "Moderate;\n Critiques Trump"))
with(data1, table(treat_republicans))


#Code treatment for Democrat Experiment 
data1$treat_democrats<-factor(data1$treat_democrats,
                              levels=c(1,2,3,4),
                              labels=c("Progressive;\n Supports Pelosi",
                                       "Progressive;\n Critiques Pelosi",
                                       "Moderate;\n Supports Pelosi",
                                       "Moderate;\n Critiques Pelosi"))
with(data1, table(treat_democrats))

#Code Party ID as Factor 
data1$pid3_lean<-factor(data1$pid3_lean,
                        levels=c(1,2,3),
                        labels=c("Democrat","Independent","Republican"))
#Remove Missing Party ID 
data1<-data1 %>% filter(pid3_lean !="NA")
with(data1, table(pid3_lean))

#Republican Analysis for Fig 1  
repub<-data1 %>% 
  filter(pid3_lean !="Independent") %>% 
  group_by(treat_republicans, pid3_lean) %>%
  summarize(mean_sc1=mean(rep_approve, na.rm=T),
            sd_sc1=sd(rep_approve, na.rm=T),
            n_sc1=n()) %>%
  mutate(se_sc1=sd_sc1/sqrt(n_sc1),
         lower_ci_sc1=mean_sc1-qt(1-(0.05/2),n_sc1-1)*se_sc1,
         upper_ci_sc1=mean_sc1+qt(1-(0.05/2),n_sc1-1)*se_sc1)
repub

#Democrat Analysis for Fig 1  
dem<-data1 %>% 
  filter(pid3_lean !="Independent") %>% 
  group_by(treat_democrats, pid3_lean) %>%
  summarize(mean_sc1=mean(dem_approve, na.rm=T),
            sd_sc1=sd(dem_approve, na.rm=T),
            n_sc1=n()) %>%
  mutate(se_sc1=sd_sc1/sqrt(n_sc1),
         lower_ci_sc1=mean_sc1-qt(1-(0.05/2),n_sc1-1)*se_sc1,
         upper_ci_sc1=mean_sc1+qt(1-(0.05/2),n_sc1-1)*se_sc1)
dem

#Figure 1 
pd <- position_dodge(.2)
x<-ggplot(dem, aes(x= treat_democrats , fill= pid3_lean , y=mean_sc1))
demplot_2018<-x+geom_errorbar(aes(ymin=lower_ci_sc1, ymax=upper_ci_sc1), color="black", position=pd, width=0, lwd=1)+
  geom_point(aes(fill= pid3_lean , shape= pid3_lean ),color="black", size=5, position=pd)+
  scale_shape_manual(values=c(21,22,23), guide = guide_legend(reverse = F))+
  scale_fill_manual(values=c("gray1","gray60","gray100"), guide = guide_legend(reverse = F))+
  theme(axis.title=element_text(size=12,face="bold"))+
  theme(axis.text=element_text(size=12))+
  xlab("")+
  ggtitle("Democrat Experiment")+
  ylab("")+
  ylab("Approval")+
  theme_bw()+
  theme(plot.title = element_text(size=16, face="bold"))+
  theme(legend.title=element_blank(), legend.position="top")+
  theme(legend.text=element_text(size=13))+
  theme(axis.title=element_text(size=16,face="bold"))+
  theme(axis.text=element_text(size=14))+
  theme(strip.text.x = element_text(size = 14))
demplot_2018

pd <- position_dodge(.2)
x<-ggplot(repub, aes(x= treat_republicans , fill= pid3_lean , y=mean_sc1))
repubplot_2018<-x+geom_errorbar(aes(ymin=lower_ci_sc1, ymax=upper_ci_sc1), color="black", position=pd, width=0, lwd=1)+
  geom_point(aes(fill= pid3_lean , shape= pid3_lean ),color="black", size=5, position=pd)+
  scale_shape_manual(values=c(21,22,23), guide = guide_legend(reverse = F))+
  scale_fill_manual(values=c("gray1","gray60","gray100"), guide = guide_legend(reverse = F))+
  theme(axis.title=element_text(size=12,face="bold"))+
  theme(axis.text=element_text(size=12))+
  xlab("")+
  ggtitle("Republican Experiment")+
  ylab("")+
  theme_bw()+
  theme(plot.title = element_text(size=16, face="bold"))+
  theme(legend.title=element_blank(), legend.position="top")+
  theme(legend.text=element_text(size=13))+
  theme(axis.title=element_text(size=16,face="bold"))+
  theme(axis.text=element_text(size=14))+
  theme(strip.text.x = element_text(size = 14))

combined_2018<-demplot_2018+repubplot_2018 & theme(legend.position="top")
combined_2018+plot_layout(guides="collect")

ggsave(filename="Fig1.tiff",
       device="tiff",
       width=15, 
       height=8, 
       dpi=100)


##2019 Qualtrics Dataset####

data2<-read.dta13("2019Qualtrics.dta")
summary(data2)

data2<-data2 %>% filter(pid3 !="NA") 
summary(data2)

data2$rep_treat<-factor(data2$rep_treat, 
                        levels=c(1,3,2,4),
                        labels=c("Conservative;\n Supports Trump",
                                 "Conservative;\n Opposes Trump",
                                 "Moderate;\n Supports Trump",
                                 "Moderate;\n Opposes Trump"))
with(data2, table(rep_treat))

data2$dem_treat<-factor(data2$dem_treat, 
                        levels=c(3,1,4,2),
                        labels=c("Progressive;\n Supports Pelosi & \nOpposes\nImpeachment",
                                 "Progressive;\n Opposes Pelosi &\nSupports\nImpeachment",
                                 "Moderate;\n Supports Pelosi &\nOpposes\nImpeachment",
                                 "Moderate;\n Opposes Pelosi &\nSupports\nImpeachment"))
with(data2, table(dem_treat))

data2$pid3<-factor(data2$pid3, 
                   levels=c(1,2,3),
                   labels=c("Democrat","Independent","Republican"))

data2b<-data2 %>% filter(pid3 !="NA")

dem_leader<-data2b %>% filter(dem_treat !="NA") %>% 
  group_by(dem_treat, pid3) %>%
  summarize(mean_sc1=mean(demsupport, na.rm=T),
            sd_sc1=sd(demsupport, na.rm=T),
            n_sc1=n()) %>%
  mutate(se_sc1=sd_sc1/sqrt(n_sc1),
         lower_ci_sc1=mean_sc1-qt(1-(0.05/2),n_sc1-1)*se_sc1,
         upper_ci_sc1=mean_sc1+qt(1-(0.05/2),n_sc1-1)*se_sc1)
dem_leader

rep_leader<-data2b %>% filter(rep_treat !="NA") %>% 
  group_by(rep_treat, pid3) %>%
  summarize(mean_sc1=mean(repsupport, na.rm=T),
            sd_sc1=sd(repsupport, na.rm=T),
            n_sc1=n()) %>%
  mutate(se_sc1=sd_sc1/sqrt(n_sc1),
         lower_ci_sc1=mean_sc1-qt(1-(0.05/2),n_sc1-1)*se_sc1,
         upper_ci_sc1=mean_sc1+qt(1-(0.05/2),n_sc1-1)*se_sc1)
rep_leader

rep_leader2 <- rep_leader %>% filter(pid3 !="Independent")
pd <- position_dodge(.2)
x<-ggplot(rep_leader2, aes(x= rep_treat , fill= pid3 , y=mean_sc1))
repplot_2019<-x+geom_errorbar(aes(ymin=lower_ci_sc1, ymax=upper_ci_sc1), color="black", position=pd, width=0, lwd=1)+
  geom_point(aes(fill= pid3 , shape= pid3 ),color="black", size=4, position=pd)+
  scale_shape_manual(values=c(21,22,23), guide = guide_legend(reverse = F))+
  scale_fill_manual(values=c("gray1","gray60","gray100"), guide = guide_legend(reverse = F))+
  theme(axis.title=element_text(size=12,face="bold"))+
  theme(axis.text=element_text(size=12))+
  xlab("")+
  ylab("")+
  labs(title="Republican Experiment")+
  scale_y_continuous(limits = c(1.75, 4.5))+
  theme_bw()+
  theme(plot.title = element_text(size=16, face="bold"))+
  theme(legend.title=element_blank(), legend.position="top")+
  theme(legend.text=element_text(size=13))+
  theme(axis.title=element_text(size=16,face="bold"))+
  theme(axis.text=element_text(size=14))+
  theme(strip.text.x = element_text(size = 14))
repplot_2019

#Dem Experiment

dem_leader2<-dem_leader %>% filter(pid3 !="Independent")

pd <- position_dodge(.2)
x<-ggplot(dem_leader2, aes(x= dem_treat , fill= pid3 , y=mean_sc1))
demplot_2019<-x+geom_errorbar(aes(ymin=lower_ci_sc1, ymax=upper_ci_sc1), color="black", position=pd, width=0, lwd=1)+
  geom_point(aes(fill= pid3 , shape= pid3 ),color="black", size=4, position=pd)+
  scale_shape_manual(values=c(21,22,23), guide = guide_legend(reverse = F))+
  scale_fill_manual(values=c("gray1","gray60","gray100"), guide = guide_legend(reverse = F))+
  theme(axis.title=element_text(size=12,face="bold"))+
  theme(axis.text=element_text(size=12))+
  xlab("")+
  ylab("Approval")+
  labs(title="Democrat Experiment")+
  scale_y_continuous(limits = c(1.75, 4.5))+
  theme_bw()+
  theme(plot.title = element_text(size=16, face="bold"))+
  theme(legend.title=element_blank(), legend.position="top")+
  theme(legend.text=element_text(size=13))+
  theme(axis.title=element_text(size=16,face="bold"))+
  theme(axis.text=element_text(size=14))+
  theme(strip.text.x = element_text(size = 14))
demplot_2019


combined_2019<-demplot_2019+repplot_2019 & theme(legend.position="top")
combined_2019+plot_layout(guides="collect")

ggsave(filename="Fig2 .tiff",
       device="tiff",
       width=15, 
       height=8, 
       dpi=100)



